package pl.mchaniewski.ryneczek.web.user;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.ComponentSystemEvent;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import pl.mchaniewski.ryneczek.pojo.Company;
import pl.mchaniewski.ryneczek.pojo.Quotation;
import pl.mchaniewski.ryneczek.services.CompanyService;
import pl.mchaniewski.ryneczek.services.QuotationService;
import pl.mchaniewski.ryneczek.utilities.DateUtil;
import pl.mchaniewski.ryneczek.utilities.I18nUtil;
import pl.mchaniewski.ryneczek.utilities.NavigationUtil;

@Component
@Scope("request")
@PreAuthorize("isAuthenticated()")
public class CompanyArchiveBean {
    private QuotationService quotationService;
    private CompanyService companyService;
    private SelectedPortfolioBean selectedPortfolioBean;
    private Company company;
    private int timePeriod;
    private CartesianChartModel dailyChange;
    private CartesianChartModel tradingVolume;
    private CartesianChartModel marketValue;

    /* PostConstruct parameters */
    private List<Quotation> latestQuotations;

    /* Request parameters */
    private String name;

    public static final int DEFAULT_TIME_PERIOD = 30;

    @Autowired
    public void setQuotationService(QuotationService quotationService) {
        this.quotationService = quotationService;
    }

    @Autowired
    public void setCompanyService(CompanyService companyService) {
        this.companyService = companyService;
    }

    @Autowired
    public void setSelectedPortfolioBean(
            SelectedPortfolioBean selectedPortfolioBean) {
        this.selectedPortfolioBean = selectedPortfolioBean;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CartesianChartModel getDailyChange() {
        return dailyChange;
    }

    public CartesianChartModel getTradingVolume() {
        return tradingVolume;
    }

    public CartesianChartModel getMarketValue() {
        return marketValue;
    }

    public Company getCompany() {
        if (company != null) {
            if (company.getName().equals(name)) {
                return company;
            }
        }

        if (name != null) {
            company = companyService.findByName(name);
            return company;
        }

        return null;
    }

    public List<Quotation> getLatestQuotations() {
        return latestQuotations;
    }

    @PostConstruct
    public void preInit() {
        if (selectedPortfolioBean.getSelectedPortfolioId() == null) {
            NavigationUtil.directRedirect(NavigationUtil.DIRECT_INDEX);
        }
    }

    public void init(ComponentSystemEvent event) {
        if (name == null || name.equals("")) {
            NavigationUtil.directRedirect(NavigationUtil.DIRECT_QUOTATION_LIST);
            return;
        }

        if (getCompany() == null) {
            NavigationUtil.directRedirect(NavigationUtil.DIRECT_QUOTATION_LIST);
            return;
        }
    }

    public String initBasedOnPathParam() {
        timePeriod = DEFAULT_TIME_PERIOD;
        initLinearModels();

        return null;
    }

    public void initLinearModels() {
        dailyChange = new CartesianChartModel();
        tradingVolume = new CartesianChartModel();
        marketValue = new CartesianChartModel();

        LineChartSeries currrentSeries = new LineChartSeries();
        currrentSeries.setLabel(I18nUtil
                .getLocalizedMessage("companydetailsbean.chartCurrent"));

        LineChartSeries marketValueSeries = new LineChartSeries();
        marketValueSeries.setLabel(I18nUtil
                .getLocalizedMessage("companydetailsbean.chartMarketValue"));

        LineChartSeries tradingVolumeSeries = new LineChartSeries();
        tradingVolumeSeries.setLabel(I18nUtil
                .getLocalizedMessage("companydetailsbean.chartVolume"));

        Date now = DateUtil.getNow();
        now = DateUtil.addDate(now, -timePeriod, 0, 0);
        for (int i = 0; i < timePeriod; ++i) {
            now = DateUtil.addDate(now, 1, 0, 0);

            Quotation lastestQuotationAt = quotationService.findLatestByDate(
                    getCompany().getCompanyid(), now);

            if (lastestQuotationAt != null) {
                currrentSeries.set(lastestQuotationAt.getFormattedDate(),
                        lastestQuotationAt.getCurrentValue());
                marketValueSeries.set(lastestQuotationAt.getFormattedDate(),
                        lastestQuotationAt.getMarketValue());
                tradingVolumeSeries.set(lastestQuotationAt.getFormattedDate(),
                        lastestQuotationAt.getTradingVolume());
            }
        }

        dailyChange.addSeries(currrentSeries);
        tradingVolume.addSeries(marketValueSeries);
        marketValue.addSeries(tradingVolumeSeries);
    }

}
